package si.wasu.drawable;

import java.util.ArrayList;
import java.util.List;

import javax.vecmath.Point2f;

import si.wasu.drawable.enums.Direction;
import android.graphics.Point;

public class Grid {
	
	// tocke samo vrhnjih koordinat trikotnikov v gridu
	private ArrayList<Point2f> allPoints = new ArrayList<Point2f>(); 
	
	// vse tocke (tudi podvojene)- npr.2. trikotnik TOP == 1. trikotnik LEFT
	private ArrayList<Point2f> allPointsMultiple = new ArrayList<Point2f>(); 

	// vse tocke DOWN trikotnikov
	private ArrayList<Point2f> allPointsDown = new ArrayList<Point2f>(); 

	// list trikotnikov z vsemi trikotniki z orientacijo UP
	private ArrayList<Triangle> gridTrianglesUp = new ArrayList<Triangle>(); 
	
	// list trikotnikov z vsemi trikotniki z orientacijo DOWN
	private ArrayList<Triangle> gridTrianglesDown = new ArrayList<Triangle>(); 

	private int height;
	private int stTrikotnikov;

	private int screenHeight, screenWidth;
	private int offsetLeft = 180;
	private int offsetCenter = 50;
	private int offsetBottom = 50;
	private int offsetTop = 50;

	private int Twidth;

	public ArrayList<Point2f> getAllPoints() {
		return allPoints;
	}

	public void setAllPoints(ArrayList<Point2f> allPoints) {
		this.allPoints = allPoints;
	}

	public Grid() {

	}

	public ArrayList<Triangle> getGridTrianglesUp() {
		return gridTrianglesUp;
	}

	public void setGridTrianglesUp(ArrayList<Triangle> gridTrianglesUp) {
		this.gridTrianglesUp = gridTrianglesUp;
	}

	public ArrayList<Point2f> getAllPointsMultiple() {
		return allPointsMultiple;
	}

	public void setAllPointsMultiple(ArrayList<Point2f> allPointsMultiple) {
		this.allPointsMultiple = allPointsMultiple;
	}

	public Grid(int h, Point size, int triangleWidth) {
		height = h;
		stTrikotnikov = h * h;
		
		screenWidth = size.x;
		screenHeight = size.y;

		offsetLeft=screenWidth/2;
		
		Twidth = triangleWidth;

		allPoints.clear();
	}

	public int getScreenHeight() {
		return screenHeight;
	}

	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}

	public int getScreenWidth() {
		return screenWidth;
	}

	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}

	private Point2f getTopPoint() {
		float x = getScreenWidth() / 2;
		float y = getOffsetTop();
		return new Point2f(x, y);
	}

	public int getOffsetTop() {
		return offsetTop;
	}

	public void setOffsetTop(int offsetTop) {
		this.offsetTop = offsetTop;
	}

	public void drawGrid() {
		Point2f top = getTopPoint(); // zacetna vrhnja tocka
		int currentNum = 1; // stevilo trikotnikov na visini 1

		ArrayList<Point2f> currentTopPoints = new ArrayList<Point2f>();
		ArrayList<Point2f> nextTopPoints = new ArrayList<Point2f>();
		currentTopPoints.add(top);
		allPoints.clear();
		allPoints.add(top);
		allPointsMultiple.clear();
		allPointsDown.clear();

		for (int i = 1; i <= height; i++) {

			for (int j = 1; j <= currentNum; j++) {
				top = currentTopPoints.get(j - 1);
				Point2f druga = getSecondPoint(top);
				Point2f tretja = getThirdPoint(top);

				allPointsMultiple.add(top);
				allPointsMultiple.add(druga);
				allPointsMultiple.add(tretja);

				if (j == currentNum) {
					nextTopPoints.add(druga);
					nextTopPoints.add(tretja);
				} else {
					nextTopPoints.add(druga);
				}

				if (i < height) {
					allPointsDown.add(druga);
					allPointsDown.add(new Point2f(top.x, top.y + (2 * Helper.getTriangleHeight(getTwidth()))));
					allPointsDown.add(tretja);
				}
			}
			currentTopPoints = (ArrayList<Point2f>) nextTopPoints.clone();
			for (Point2f point2f : nextTopPoints) {
				allPoints.add(point2f);
			}
			nextTopPoints.clear();
			currentNum++; // na vsaki visini se st. trikotnikov poveca za 1 -
							// tistih ki jih izrisemo
		}
		makeTriangles();
	}

	public ArrayList<Point2f> getAllPointsDown() {
		return allPointsDown;
	}

	public void setAllPointsDown(ArrayList<Point2f> allPointsDown) {
		this.allPointsDown = allPointsDown;
	}

	private void makeTriangles() {
		gridTrianglesUp.clear();
		for (int i = 0; i < allPointsMultiple.size(); i += 3) {
			gridTrianglesUp.add(new Triangle(allPointsMultiple.get(i), allPointsMultiple.get(i + 1), allPointsMultiple.get(i + 2), Direction.UP, getTwidth()));
		}

		gridTrianglesDown.clear();
		for (int i = 0; i < allPointsDown.size(); i += 3) {
			gridTrianglesDown.add(new Triangle(allPointsDown.get(i), allPointsDown.get(i + 1), allPointsDown.get(i + 2), Direction.DOWN, getTwidth()));
		}

	}

	public ArrayList<Triangle> getGridTrianglesDown() {
		return gridTrianglesDown;
	}

	public void setGridTrianglesDown(ArrayList<Triangle> gridTrianglesDown) {
		this.gridTrianglesDown = gridTrianglesDown;
	}

	private Point2f getSecondPoint(Point2f top) {
		return new Point2f(top.x - getTwidth() / 2f, top.y + Helper.getTriangleHeight(getTwidth()));
	}

	private Point2f getThirdPoint(Point2f top) {
		return new Point2f(top.x + getTwidth() / 2f, top.y + Helper.getTriangleHeight(getTwidth()));
	}

	public int getTwidth() {
		return Twidth;
	}

	public void setTwidth(int twidth) {
		Twidth = twidth;
	}

	public List<Triangle> getGridTriangles() {
		List<Triangle> newList = new ArrayList<Triangle>(gridTrianglesDown);
		newList.addAll(gridTrianglesUp);
		return newList;
	}
}
